package com.java.business.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.java.business.entity.Category;
import com.java.business.service.CategoryService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;



/**
 * 分类
 *
 * @author jl
 * @since 2020-10-07 10:31:35
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表查询
     */
    @PostMapping("/all")
    public R all() {
        List<Category> categoryDtoList = categoryService.list(null);
        return R.ok(categoryDtoList);
    }

    /**
     * 带条件分页查询分类
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

	    return R.ok(page);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R updateOrSave(@RequestBody Category category) {
        if (StrUtil.isNotBlank(category.getId())) {
            categoryService.updateById(category);
        } else {
            category.setId(UuidUtil.getShortUuid());
            categoryService.save(category);
        }
        return R.ok(category);
    }


    /**
     * 删除分类
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
		categoryService.deleteById(id);

        return R.ok();
    }

}
